package com.star.service.impl;


import com.star.exception.NotFoundException;
import com.star.mapper.BlogMapper;
import com.star.pojo.Blog;
import com.star.service.BlogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 博客列表业务层接口实现类
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Resource
    private BlogMapper blogMapper;


    @Override
    public List<Blog> BlogList() {
        return blogMapper.BlogList();
    }

    @Override
    public int saveBlog(Blog blog) {

        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());

        blog.setViews(0);

        blog.setCommentCount(0);

        return blogMapper.saveBlog(blog);
    }

    @Override
    public Blog getBlogById(Long id) {
        return blogMapper.getBlogById(id);
    }

    @Override
    public int updateBlog(Blog blog) {
        if (blog.getId()==null){
            throw  new NotFoundException("不存在博客");
        }
        blog.setUpdateTime(new Date());

        return blogMapper.updateBlog(blog);
    }

    @Override
    public void deleteBlog(Long id) {
        blogMapper.deleteBlog(id);
    }


}